<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<title>wl101用户管理</title>
	<link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
	<link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
	<div class="layuimini-container">
		<div class="layuimini-main">

			<fieldset class="table-search-fieldset">
				<legend>搜索信息</legend>
				<div style="margin: 10px 10px 10px 10px">
					<form class="layui-form layui-form-pane" action="">
						<div class="layui-form-item">
							<div class="layui-inline">
								<label class="layui-form-label">用户ID</label>
								<div class="layui-input-inline">
									<input type="text" name="userid" autocomplete="off" class="layui-input"
										placeholder="" maxlength="5">
								</div>
							</div>
							<div class="layui-inline">
								<label class="layui-form-label">用户名</label>
								<div class="layui-input-inline">
									<input type="text" name="username" autocomplete="off" class="layui-input"
										placeholder="模糊查询" maxlength="12">
								</div>
							</div>
							<div class="layui-inline">
								<button type="submit" class="layui-btn layui-btn-primary" lay-submit
									lay-filter="data-search-btn" id="btnSearch"><i class="layui-icon"></i> 搜
									索</button>
							</div>
						</div>
					</form>
				</div>
			</fieldset>

			<script type="text/html" id="wl101Toolbar">
				<div class="layui-btn-container">
					<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加</button>
					<button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete">删除</button>
				</div>
			</script>

			<table class="layui-hide" id="wl101TableId" lay-filter="wl101Filter"></table>

			<script type="text/html" id="wl101TableBar">
				<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
			</script>

		</div>
	</div>
	<script src="../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
	<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
	<script>
	
		function refresh(userid) {
			$('#btnSearch').trigger("click");
			if (userid==1){
				layer.msg('用户添加成功~');
			} else{
				layer.msg('用户编辑成功~');
			}
		}
		
		layui.use(['jquery', 'layer', 'form'], function() {
			var $ = layui.jquery,
				form = layui.form,
				table = layui.table;

			table.render({
				elem: '#wl101TableId',
				url: '../server/wl101_search.php',
				toolbar: '#wl101Toolbar',
				defaultToolbar: ['filter', 'exports', 'print', {
					title: '提示',
					layEvent: 'LAYTABLE_TIPS',
					icon: 'layui-icon-tips'
				}],
				cols: [
					[{
							type: "checkbox",
							width: 50
						},
						{
							field: 'user_id',
							width: '10%',
							title: '用户ID',
							sort: true
						},
						{
							field: 'user_login',
							width: '15%',
							title: '登录账号'
						},
						{
							field: 'user_name',
							width: '15%',
							title: '用户名'
						},
						{
							field: 'user_tel',
							width: '20%',
							title: '手机号'
						},
						{
							field: 'user_remark',
							width: '20%',
							title: '备注'
						},
						{
							title: '操作',
							minWidth: 150,
							toolbar: '#wl101TableBar',
							align: "center"
						}
					]
				],
				limits: [50, 100, 200, 500, 1000],
				limit: 50,
				page: true,
				skin: 'line'
			});

			//监听搜索操作
			form.on('submit(data-search-btn)', function(data) {

				console.log('检索');
				//执行搜索重载
				table.reload('wl101TableId', {
					url: '../server/wl101_search.php',
					where: {
						companyid: $("#company_id", parent.document).val(),
						userid: data.field.userid,
						username: data.field.username
					}
				}, 'data');

				return false;
			});

			/**
			 * toolbar监听事件
			 */
			table.on('toolbar(wl101Filter)', function(obj) {
				if (obj.event === 'add') {
					var index = layer.open({
						title: '添加用户',
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area: ['100%', '100%'],
						content: 'wl101add.html'
					});
					$(window).on("resize", function() {
						layer.full(index);
					});
				} else if (obj.event === 'delete') {

					var checkStatus = table.checkStatus('wl101TableId'),
						data = checkStatus.data;

					if (JSON.stringify(data) == '[]') {
						layer.msg('请选择要删除的数据行~');
					} else {

						var applist = new Array();
						for (var i = 0; i < data.length; i++) {
							var app = new Array();
							applist[i] = data[i].user_id;
						}

						$.ajax({
							url: '../server/wl101_delete_batch.php',
							type: 'POST',
							dataType: 'json',
							data: {
								list: JSON.stringify(applist)
							},
							success: function(data) {
								if (data.success) {
									layer.msg('删除成功~');
									$('#btnSearch').trigger("click");
								} else {
									layer.alert('删除失败~', {
										title: '提示',
										icon: 5
									});
								}
							}
						})
					}

				}
			});

			table.on('tool(wl101Filter)', function(obj) {
				var data = obj.data;
				if (obj.event === 'edit') {
					var index = layer.open({
						title: '编辑用户',
						type: 2,
						shade: 0.2,
						maxmin: true,
						shadeClose: true,
						area: ['100%', '100%'],
						content: 'wl101edit.html',
						success: function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.find('#user_id').val(data['user_id']);
						}
					});
					$(window).on("resize", function() {
						layer.full(index);
					});
					return false;
					
				} else if (obj.event === 'delete') {

					layer.confirm('真的删除行么~', {
						title: '提示'
					}, function(index) {
						$.ajax({
							url: '../server/wl101_delete.php',
							type: 'POST',
							dataType: 'json',
							data: {
								user_id: data['user_id'],
							},
							success: function(data) {
								if (data.success) {
									layer.msg('删除成功~');
									obj.del();
								} else {
									layer.alert('删除失败~', {
										title: '提示',
										icon: 5
									});
								}
							}
						})
						layer.close(index);
					});

				}
			});

		});
	</script>
</body>
</html>
